package com.siao.example.loading.model
{
	import com.siao.core.AssetsManager;
	import com.siao.data.XmlValue;
	import com.siao.events.BytesEvent;
	import com.siao.example.loading.method.LoadMethod;
	import com.siao.loader.BytesLoader;
	import com.siao.loader.BytesQueueLoader;
	import com.siao.mvc.Model;
	
	import flash.events.ProgressEvent;
	
	/**
	 * 资源加载数据模型
	 * @author SiaoLeon
	 */
	public class AssetsLoadModel extends Model
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		private var _countTotal:int;
		
		/**
		 * 加载资源总数
		 * @return
		 */
		public function get countTotal():int
		{
			return _countTotal;
		}
		
		private var _countLoaded:int;
		
		/**
		 * 已加载资源个数
		 * @return
		 */
		public function get countLoaded():int
		{
			return _countLoaded;
		}
		
		private var _currentLoaded:int;
		
		/**
		 * 当前资源已加载量
		 * @return
		 */
		public function get currentLoaded():int
		{
			return _currentLoaded;
		}
		
		private var _currentTotal:int;
		
		/**
		 * 当前资源总加载量
		 * @return
		 */
		public function get currentTotal():int
		{
			return _currentTotal;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var loader:BytesQueueLoader;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function AssetsLoadModel()
		{
			super();
			
			this.loader = new BytesQueueLoader();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function onAdd():void
		{
			this.loader.addEventListener(BytesEvent.OPEN, onOpen);
			this.loader.addEventListener(BytesEvent.ERROR, onError);
			this.loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			this.loader.addEventListener(BytesEvent.COMPLETE, onComplete);
		}
		
		override public function onRemove():void
		{
			this.loader.removeEventListener(BytesEvent.OPEN, onOpen);
			this.loader.removeEventListener(BytesEvent.ERROR, onError);
			this.loader.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			this.loader.removeEventListener(BytesEvent.COMPLETE, onComplete);
		}
		
		/**
		 * 加载列表中的所有资源
		 * @param list 资源列表
		 */
		public function load(list:Vector.<XmlValue>):void
		{
			for each (var info:XmlValue in list)
			{
				this.loader.load(info.value, info);
			}
			
			this._countTotal = list.length;
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		private function onOpen(event:BytesEvent):void
		{
			this._currentLoaded = 0;
			this._currentTotal = 0;
			this._countLoaded = this._countTotal - this.loader.length - 1;
		}
		
		private function onError(event:BytesEvent):void
		{
			this.notify(LoadMethod.LOAD_ERROR, (event.target as BytesLoader).params);
		}
		
		private function onProgress(event:ProgressEvent):void
		{
			this._currentLoaded = event.bytesLoaded;
			this._currentTotal = event.bytesTotal;
			
			this.notify(LoadMethod.LOAD_PROGRESS);
		}
		
		private function onComplete(event:BytesEvent):void
		{
			AssetsManager.instance.saveAsset((this.loader.params as XmlValue).name, this.loader.content);
			
			if (this.loader.length < 1)
			{
				this.notify(LoadMethod.LOAD_COMPLETE);
			}
		}
	
	}
}
